//
//  JQFXHHomeViewController.swift
//  jiuzhoudai
//
//  Created by zhaochengfeng on 2025/7/25.
//

import Foundation
import UIKit
import SnapKit

public let jqfxhScreenWidth = UIScreen.main.bounds.size.width

public let jqfxhScreenHeight = UIScreen.main.bounds.size.height

class JQFXHHomeViewController: HJYJBaseViewController {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.jqfxhBigViewBgccGray
        self.jqfxhIsNavBarjjHidden = true
        self.jqfxhSetffUI()
        
    }
    
    
    @objc func jqfxhShenqingppBtnClick() {
        if JQFXHNetworkManager.shared.isLogin {
            self.jqfxhBeforeLoanrrInfo()
        }else {
            self.jqfxhTologinuuVC()
        }
        
        
    }
    
    
    func jqfxhSetffUI() -> Void {
        self.view.backgroundColor = UIColor.jqfxhBigViewBgccGray
        self.view.addSubview(jqfxhScrollggview)
        jqfxhScrollggview.addSubview(jqfxhContentkkView)
        jqfxhScrollggview.snp.makeConstraints { make in
            make.edges.equalTo(self.view)
        }
        jqfxhContentkkView.snp.makeConstraints { make in
            make.top.bottom.equalTo(jqfxhScrollggview)
            make.left.right.equalTo(self.view)
        }
        
       
        
        jqfxhContentkkView.addSubview(jqfxhTopBgxxImg)
        jqfxhTopBgxxImg.snp.makeConstraints { make in
            make.left.right.top.equalTo(jqfxhContentkkView)
        }
        
        jqfxhContentkkView.addSubview(jqfxhTopCardppBg)
        jqfxhContentkkView.addSubview(jqfxhTopCardllImg)
        jqfxhTopCardllImg.snp.makeConstraints { make in
            make.left.equalTo(self.jqfxhContentkkView).offset(16)
            make.right.equalTo(self.jqfxhContentkkView).offset(-16)
            make.top.equalTo(UIApplication.shared.jqfxhSafeAreaInsets.top + 50)
        }
        
        jqfxhTopCardppBg.snp.makeConstraints { make in
            make.left.equalTo(jqfxhTopCardllImg).offset(-1)
            make.right.equalTo(jqfxhTopCardllImg).offset(1)
            make.top.equalTo(jqfxhTopCardllImg).offset(-1)
            make.bottom.equalTo(jqfxhTopCardllImg).offset(55)

        }
        
    
        jqfxhTopCardllImg.addSubview(jqfxhYuguCountnnTitle)
        jqfxhYuguCountnnTitle.snp.makeConstraints { make in
            make.left.equalTo(jqfxhTopCardllImg).offset(16)
            make.top.equalTo(jqfxhTopCardllImg).offset(25)
        }
        
        jqfxhTopCardllImg.addSubview(jqfxhCountooTitle)
        jqfxhCountooTitle.snp.makeConstraints { make in
            make.top.equalTo(jqfxhYuguCountnnTitle.snp.bottom)
            make.left.equalTo(jqfxhYuguCountnnTitle)
        }
        
        
        jqfxhTopCardllImg.addSubview(jqfxhNianhuaqqTitle)
        jqfxhNianhuaqqTitle.snp.makeConstraints { make in
            make.left.equalTo(self.jqfxhYuguCountnnTitle)
            make.top.equalTo(jqfxhCountooTitle.snp.bottom).offset(5)
        }
        
        jqfxhTopCardllImg.addSubview(jqfxhMoneyCompanyrrLabel)
        jqfxhMoneyCompanyrrLabel.snp.makeConstraints { make in
            make.centerX.equalTo(jqfxhTopCardllImg)
            make.top.equalTo(self.jqfxhNianhuaqqTitle.snp.bottom).offset(10)
        }
        
        
        jqfxhTopCardllImg.addSubview(jqfxhShenqingppBtn)
        jqfxhShenqingppBtn.snp.makeConstraints { make in
            make.left.equalTo(jqfxhYuguCountnnTitle)
            make.centerX.equalTo(self.jqfxhTopCardllImg)
            make.top.equalTo(jqfxhMoneyCompanyrrLabel.snp.bottom).offset(10)
            make.bottom.equalTo(jqfxhTopCardllImg).offset(-20)

            make.height.equalTo(29)
        }
        
        
        
        
        self.jqfxhContentkkView.addSubview(jqfxhZhissNanBtn)
        jqfxhZhissNanBtn.snp.makeConstraints { make in
            make.right.equalTo(jqfxhTopCardllImg)
            make.top.equalTo(jqfxhTopCardllImg.snp.bottom).offset(15)
            make.width.equalTo(jqfxhTopCardllImg.snp.width).multipliedBy(0.25)
            make.height.equalTo(34)
        }
        
        self.jqfxhContentkkView.addSubview(jqfxhXiqqfeiBtn)
        jqfxhXiqqfeiBtn.snp.makeConstraints { make in
            make.right.equalTo(jqfxhZhissNanBtn.snp.left)
            make.top.width.height.equalTo(jqfxhZhissNanBtn)
        }
        
        
        self.jqfxhContentkkView.addSubview(jqfxhTrrkBtn)
        jqfxhTrrkBtn.snp.makeConstraints { make in
            make.right.equalTo(jqfxhXiqqfeiBtn.snp.left)
            make.top.width.height.equalTo(jqfxhZhissNanBtn)
        }
        
        
        self.jqfxhContentkkView.addSubview(jqfxhShenqinglcttLabel)
        jqfxhShenqinglcttLabel.snp.makeConstraints { make in
            make.left.equalTo(jqfxhTopCardllImg)
            make.top.equalTo(jqfxhTopCardllImg.snp.bottom).offset(86)
        }
        
        let jqfxhContentvvHeight = (jqfxhScreenWidth-32)*217/339

        self.jqfxhContentkkView.addSubview(jqfxhShenqinguuImg)
        jqfxhShenqinguuImg.snp.makeConstraints { make in
            make.left.right.equalTo(jqfxhTopCardllImg)
            make.top.equalTo(jqfxhShenqinglcttLabel.snp.bottom).offset(10)
            make.bottom.equalTo(jqfxhContentkkView).offset(-40)
            make.height.equalTo(jqfxhContentvvHeight)
        }
        
        
        
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.jqfxhUpdateServicettInfo()
        
    }
    
    @objc func jqfxhUpdateServicettInfo() {
        let provider = JQFXHNetworkManager.shared.provider
        JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhServicehhInfo, type:JQFXHServerddInfoData.self) { result in
            switch result {
            case.success(let serverInfo):
                if serverInfo.app_config.partner_id != JQFXHNetworkManager.shared.partnerId {
                    JQFXHNetworkManager.shared.jqfxhCurrentUser = nil
                    JQFXHNetworkManager.shared.currentServerInfo = serverInfo
                }
            case .failure(let error):
                print(error)
            }
        }
    }
    
    func jqfxhTologinuuVC() -> Void {
        JQFXHHUD.jqfxhShowssLoading()
        let provider = JQFXHNetworkManager.shared.provider
        JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhServicehhInfo, type:JQFXHServerddInfoData.self) { result in
            JQFXHHUD.jqfxhHttidden()
            switch result {
            case.success(let serverInfo):
                JQFXHNetworkManager.shared.currentServerInfo = serverInfo
                let loginvc = JQFXHLoginViewController.init()
                self.navigationController?.pushViewController(loginvc, animated: true)
            case .failure(let error):
                print(error)
            }
        }
        
    }
    
    @objc func jqfxhTrrkBtnClick() {
        
        let web = JQFXHTermsViewController.init()
        self.present(web, animated: true)
       
    }
    
    
    @objc func jqfxhZhiNamwwBtnClick() {

        let knowledge = JQFXHLoanKnowlagemmViewController.init()
        self.navigationController?.pushViewController(knowledge, animated: true)
    }
    
    @objc func jqfxhXiqqfeiBtnClick() {
        let calcu = JQFXHCalculationViewController.init()
        self.navigationController?.pushViewController(calcu, animated: true)
    }

    @objc func jqfxhBeforeLoanrrInfo() {
        JQFXHHUD.jqfxhShowssLoading()
        let provider = JQFXHNetworkManager.shared.provider
        JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhServicehhInfo, type:JQFXHServerddInfoData.self) { result in
            JQFXHHUD.jqfxhHttidden()
            switch result {
            case.success(let serverInfo):
                if serverInfo.app_config.partner_id != JQFXHNetworkManager.shared.partnerId {
                    JQFXHNetworkManager.shared.jqfxhCurrentUser = nil

                    JQFXHNetworkManager.shared.currentServerInfo = serverInfo
                    let loginvec = JQFXHLoginViewController.init()
                    self.navigationController?.pushViewController(loginvec, animated: true)
                }else {
                    let realNameVC = JQFXHRealNamessViewController()
                    self.navigationController?.pushViewController(realNameVC, animated: true)
                }
            case .failure(let error):
                print(error)
            }
        }
    }
    
    lazy var jqfxhScrollggview : UIScrollView = {
        let jqfxhScrollggview = UIScrollView(frame: CGRectMake(0, 0, jqfxhScreenWidth, jqfxhScreenHeight))
        if #available(iOS 11.0, *) {
            jqfxhScrollggview.contentInsetAdjustmentBehavior = .never
        } else {
        }
        return jqfxhScrollggview
    }()
    
    lazy var jqfxhContentkkView : UIView = {
        let jqfxhContentkkView = UIView()
        
        return jqfxhContentkkView
    }()
    
    lazy var jqfxhTopBgxxImg : UIImageView = {
        let jqfxhTopBgxxImg = UIImageView(image: UIImage(named: "jqfxh_home_top"))
        jqfxhTopBgxxImg.isUserInteractionEnabled = true
        return jqfxhTopBgxxImg
    }()
    
    lazy var jqfxhTopCardppBg : UIView = {
        let jqfxhTopCardppBg = UIView()
        jqfxhTopCardppBg.backgroundColor = UIColor.white
        jqfxhTopCardppBg.layer.cornerRadius = 12
        jqfxhTopCardppBg.layer.masksToBounds = true
        return jqfxhTopCardppBg
    }()
    
    
    lazy var jqfxhTopCardllImg : UIImageView = {
        let jqfxhTopCardllImg = UIImageView(image: UIImage(named: "jqfxh_home_card"))
        jqfxhTopCardllImg.backgroundColor = UIColor.white
        jqfxhTopCardllImg.layer.cornerRadius = 4
        jqfxhTopCardllImg.layer.masksToBounds = true
        jqfxhTopCardllImg.isUserInteractionEnabled = true
        return jqfxhTopCardllImg
    }()
    
    
    
    lazy var jqfxhHomeTitlemmLabel : UILabel = {
        let jqfxhHomeTitlemmLabel = UILabel()
        jqfxhHomeTitlemmLabel.font = UIFont.jqfxhBold22
        jqfxhHomeTitlemmLabel.textColor = UIColor.black
        jqfxhHomeTitlemmLabel.text = "借款"
        return jqfxhHomeTitlemmLabel
    }()

    
    
    
    lazy var jqfxhYuguCountnnTitle : UILabel = {
        let jqfxhYuguCountnnTitle = UILabel()
        jqfxhYuguCountnnTitle.font = UIFont.jqfxhRegular14
        jqfxhYuguCountnnTitle.textColor = UIColor.white
        jqfxhYuguCountnnTitle.text = "最高可担保额度（元）"
        return jqfxhYuguCountnnTitle
    }()
    
    
    
    
    
    lazy var jqfxhShenqingppBtn : UIButton = {
        let jqfxhShenqingppBtn = UIButton(type: .custom)
        jqfxhShenqingppBtn.setTitle("立即申请", for: .normal)
        jqfxhShenqingppBtn.setTitleColor(UIColor.jqfxhMaineeRed, for: .normal)
        jqfxhShenqingppBtn.backgroundColor = UIColor.white
        jqfxhShenqingppBtn.titleLabel?.font = UIFont.jqfxhRegular18
        jqfxhShenqingppBtn.addTarget(self, action: #selector(jqfxhShenqingppBtnClick), for: .touchUpInside)
        jqfxhShenqingppBtn.layer.cornerRadius = 4
        jqfxhShenqingppBtn.layer.masksToBounds = true
        return jqfxhShenqingppBtn
        
    }()
    
    lazy var jqfxhShenqinglcttLabel : UILabel = {
        let jqfxhShenqinglcttLabel = UILabel()
        jqfxhShenqinglcttLabel.font = UIFont.jqfxhMedium18
        jqfxhShenqinglcttLabel.textColor = UIColor.white
        jqfxhShenqinglcttLabel.text = "申请流程"
        return jqfxhShenqinglcttLabel
    }()
    
    
    lazy var jqfxhNianhuaqqTitle : UILabel = {
        let jqfxhNianhuaqqTitle = UILabel()
        jqfxhNianhuaqqTitle.font = UIFont.jqfxhRegular12
        jqfxhNianhuaqqTitle.textColor = UIColor.white
        jqfxhNianhuaqqTitle.text = "年化利率3.6%~23%"
        return jqfxhNianhuaqqTitle
    }()
    
    lazy var jqfxhCountooTitle : UILabel = {
        let jqfxhCountooTitle = UILabel()
        jqfxhCountooTitle.font = UIFont.jqfxhRegular40
        jqfxhCountooTitle.textColor = UIColor.white
        jqfxhCountooTitle.text = "200,000"
        return jqfxhCountooTitle
    }()
    
    
    lazy var jqfxhMoneyCompanyrrLabel : UILabel = {
        let jqfxhMoneyCompanyrrLabel = UILabel()
        jqfxhMoneyCompanyrrLabel.font = UIFont.jqfxhRegular12
        jqfxhMoneyCompanyrrLabel.textColor = UIColor.white
        jqfxhMoneyCompanyrrLabel.numberOfLines = 0
        let string = "本平台资金由" + JQFXHCompanyMessage.jqfxhMoneyCompanyName + "提供"
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center
        paragraphStyle.lineSpacing = 3
        jqfxhMoneyCompanyrrLabel.attributedText = NSMutableAttributedString(string: string, attributes: [.paragraphStyle:paragraphStyle])
        return jqfxhMoneyCompanyrrLabel
    }()
    
    
    lazy var jqfxhZhissNanBtn : UIButton = {
        let jqfxhZhissNanBtn = UIButton(type: .custom)
        jqfxhZhissNanBtn.setTitle("指南", for: .normal)
        jqfxhZhissNanBtn.setTitleColor(UIColor.black, for: .normal)
        jqfxhZhissNanBtn.titleLabel?.font = UIFont.jqfxhMedium14
        jqfxhZhissNanBtn.addTarget(self, action: #selector(jqfxhZhiNamwwBtnClick), for: .touchUpInside)
        return jqfxhZhissNanBtn
        
    }()
    
    lazy var jqfxhXiqqfeiBtn : UIButton = {
        let jqfxhXiqqfeiBtn = UIButton(type: .custom)
        jqfxhXiqqfeiBtn.setTitle("息费", for: .normal)
        jqfxhXiqqfeiBtn.setTitleColor(UIColor.black, for: .normal)
        jqfxhXiqqfeiBtn.titleLabel?.font = UIFont.jqfxhMedium14
        jqfxhXiqqfeiBtn.addTarget(self, action: #selector(jqfxhXiqqfeiBtnClick), for: .touchUpInside)
        return jqfxhXiqqfeiBtn
        
    }()
    
    
    lazy var jqfxhTrrkBtn : UIButton = {
        let jqfxhTrrkBtn = UIButton(type: .custom)
        jqfxhTrrkBtn.setTitle("条款", for: .normal)
        jqfxhTrrkBtn.setTitleColor(UIColor.black, for: .normal)
        jqfxhTrrkBtn.titleLabel?.font = UIFont.jqfxhMedium14
        jqfxhTrrkBtn.addTarget(self, action: #selector(jqfxhTrrkBtnClick), for: .touchUpInside)
        return jqfxhTrrkBtn
        
    }()
   
 
    
    lazy var jqfxhShenqinguuImg : UIImageView = {
        let jqfxhShenqinguuImg = UIImageView(image: UIImage(named: "hjfxh_buzou_tip"))
        return jqfxhShenqinguuImg
    }()
    
   
    
   
    
    
}


